package org.tis.tools.abf.module.ac.controller;

import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.tis.tools.abf.module.ac.controller.request.AcOperatorDefaultIdentity;
import org.tis.tools.abf.module.ac.controller.request.AcOperatorIdentityRequest;
import org.tis.tools.abf.module.ac.entity.AcOperatorIdentity;
import org.tis.tools.abf.module.ac.service.IAcOperatorIdentityService;
import org.tis.tools.core.web.controller.BaseController;
import org.tis.tools.core.web.vo.SmartPage;
import org.tis.tools.model.common.ResultVO;
import org.tis.tools.model.log.enums.OperateType;
import org.tis.tools.starter.operatelog.annotation.OperateLog;

import javax.validation.constraints.NotBlank;

/**
 * acOperatorIdentity的Controller类
 * 操作员身份
 * @author Auto Generate Tools
 * @date 2018/04/23
 */
@RestController
@Validated
@RequestMapping("/acOperatorIdentity")
@Slf4j
public class AcOperatorIdentityController extends BaseController<AcOperatorIdentity>  {

    @Autowired
    private IAcOperatorIdentityService acOperatorIdentityService;


    @ApiOperation(value = "新增操作员身份",notes = "传入AcOperatorIdentityRequest类作为参数")
    @OperateLog(type = OperateType.ADD,desc = "新增操作员身份")
    @PostMapping
    public ResultVO add(@RequestBody @Validated @ApiParam(value = "AcOperatorIdentityRequest",name = "传入的AcOperatorIdentityRequest类",required = true) AcOperatorIdentityRequest acOperatorIdentityRequest) {
        acOperatorIdentityService.add(acOperatorIdentityRequest);
        return ResultVO.success("新增成功！");
    }


    @ApiOperation(value = "修改操作员身份",notes = "传入AcOperatorIdentityRequest类作为参数")
    @OperateLog(type = OperateType.UPDATE,desc = "修改操作员身份")
    @PutMapping
    public ResultVO update(@RequestBody @Validated @ApiParam(value = "AcOperatorIdentityRequest",name = "传入的AcOperatorIdentityRequest类",required = true) AcOperatorIdentityRequest acOperatorIdentityRequest) {
        AcOperatorIdentity acOperatorIdentity = acOperatorIdentityService.selectById(acOperatorIdentityRequest.getGuid());
        if (acOperatorIdentity == null) {
            return ResultVO.error("404", "找不到对应记录或已经被删除！");
        }
        acOperatorIdentity =acOperatorIdentityService.change(acOperatorIdentityRequest);
        return ResultVO.success("修改成功！",acOperatorIdentity);
    }


    @ApiOperation(value = "删除操作员身份",notes = "传操作员身份GUID作为参数")
    @OperateLog(type = OperateType.DELETE,desc = "删除操作员身份")
    @DeleteMapping("/{id}")
    public ResultVO delete(@PathVariable @NotBlank(message = "id不能为空") @ApiParam(value = "id",name = "操作员身份的GUID", required = true) String id) {
        AcOperatorIdentity acOperatorIdentity = acOperatorIdentityService.selectById(id);
        if (acOperatorIdentity == null) {
            return ResultVO.error("404", "找不到对应记录或已经被删除！");
        }
        acOperatorIdentityService.moveIdentity(id);
        return ResultVO.success("删除成功");
    }


    @ApiOperation(value = "根据GUID查询操作员身份",notes = "传操作员身份GUID作为参数")
    @GetMapping("/{id}")
    public ResultVO detail(@PathVariable @NotBlank(message = "id不能为空") @ApiParam(value = "id",name = "操作员身份的GUID", required = true) String id) {
        AcOperatorIdentity acOperatorIdentity = acOperatorIdentityService.selectById(id);
        if (acOperatorIdentity == null) {
            return ResultVO.error("404", "找不到对应记录或已经被删除！");
        }
        return ResultVO.success("查询成功", acOperatorIdentity);
    }


    @ApiOperation(value = "分页查询操作员身份信息",notes = "传入分页信息作为参数")
    @PostMapping("/list")
    public ResultVO list(@RequestBody @Validated @ApiParam(value = "page",name = "分页信息", required = true) SmartPage<AcOperatorIdentity> page) {
        return  ResultVO.success("查询成功", acOperatorIdentityService.selectPage(page.getPageObj(), page.getConditionWrapper()));
    }


    @ApiOperation(value = "根据操作员id分页查询操作员身份信息",notes = "传入分页信息和操作员GUID作为参数")
    @PostMapping("/queryByOperator/{id}")
    public ResultVO queryByOperator(@RequestBody @Validated @ApiParam(value = "page",name = "分页信息", required = true) SmartPage<AcOperatorIdentity> page,@PathVariable @NotBlank(message = "操作员ID不能为空") @ApiParam(value = "id",name = "操作员GUID", required = true) String id){
        return ResultVO.success("查询成功",acOperatorIdentityService.queryByOperator(page.getPageObj(),page.getConditionWrapper(),id));
    }


    @ApiOperation(value = "修改操作员默认身份",notes = "传入AcOperatorDefaultIdentity类作为参数")
    @PutMapping("setDefaultIdentity")
    public ResultVO setDefaultOperatorIdentity(@RequestBody @Validated @ApiParam(value = "AcOperatorDefaultIdentity",name = "传入的AcOperatorDefaultIdentity类", required = true) AcOperatorDefaultIdentity acDefault){
        return ResultVO.success("修改默认身份成功",acOperatorIdentityService.changeDefaultOperatorIdentity(acDefault));
    }


}

